<?php
App::import ( 'Vendor', 'php-excel-reader/excel_reader' ); //import statement
App::import ( 'Vendor', 'ExcelWriter', array ('file' => 'excelwriter.inc.php' ) );

class ExcelComponent extends Object {
	
	function saveData(&$controller, $file) {
		
		if (strpos ( $controller->referer (), 'apartments' ))
			$ref_controller = 'Apartment';
		if (strpos ( $controller->referer (), 'houses' ))
			$ref_controller = 'House';
		if (strpos ( $controller->referer (), 'businesses' ))
			$ref_controller = 'Business';
		
		$this->_data = new Spreadsheet_Excel_Reader ( $file );
		$arr = array ();
		$arr = $this->_data->dumptoarray ();
		
		$array = array ();
		
		$data = $controller->$ref_controller->_schema;
		$title = array ();
		foreach ( $data as $k => $v ) {
			$title [] = $k;
		}
		foreach ( $arr as $key => $value ) {
			if ($key == 1) {
				$result1 = array_diff ( $value, $title );
				$result2 = array_diff ( $title, $value );
			
			}
		}
		
		$size = sizeof ( $result2 );
		
		if (empty ( $result1 ) && empty ( $result2 ) || ($size = 1 && $result2 [0] = 'id')) {
			foreach ( $arr as $key => $value ) {
				if ($key == 1)
					continue;
				$array = array_combine ( $arr [1], $value );
				
				$exploded = explode('/', $array['info_date']);
				if(count($exploded) == 3)
					$array['info_date'] = $exploded[2].'-'.$exploded[0].'-'.$exploded[1];
				if(strlen($array['info_date']) != 10)
					$array['info_date'] = '0000-00-00';
				
				$exploded = explode('/', $array['date_of_renting']);
				if(count($exploded) == 3)
					$array['date_of_renting'] = $exploded[2].'-'.$exploded[0].'-'.$exploded[1];
				if(strlen($array['date_of_renting']) != 10)
					$array['date_of_renting'] = '0000-00-00';
				
				$exploded = explode('/', $array['moving_date']);
				if(count($exploded) == 3)
					$array['moving_date'] = $exploded[2].'-'.$exploded[0].'-'.$exploded[1];
				if(strlen($array['moving_date']) != 10)
					$array['moving_date'] = '0000-00-00';
				
				$exploded = explode('/', $array['looking_date']);
				if(count($exploded) == 3)
					$array['looking_date'] = $exploded[2].'-'.$exploded[0].'-'.$exploded[1];
				if(strlen($array['looking_date']) != 10)
					$array['looking_date'] = '0000-00-00';

				$controller->$ref_controller->create ();
				foreach ($array as &$element){
					$element = trim($element);
				}
				$controller->$ref_controller->save ( $array );
			}
		}
		//	else echo "???????" ; 
	

	}
	
	function exportData(&$controller, $file) {
		
		if (strpos ( $controller->referer (), 'apartments' ))
			$ref_controller = 'Apartment';
		if (strpos ( $controller->referer (), 'houses' ))
			$ref_controller = 'House';
		if (strpos ( $controller->referer (), 'businesses' ))
			$ref_controller = 'Business';
		
		$data = $controller->$ref_controller->_schema;
		$title = array ();
		foreach ( $data as $k => $v ) {
			$title [] = $k;
		}
		
		$this->_data = new ExcelWriter ( $file );
		$this->_data->writeLine ( $title );
		
		if ($this->_data == false)
			echo $this->_data->error;
		
		$data = $controller->$ref_controller->find ( 'all' );
		
		foreach ( $data as $value ) {
			foreach ( $value as $row ) {
				$this->_data->writeRow ();
				foreach ( $row as $col )
					$this->_data->writeCol ( $col );
			}
		}
		$this->_data->close ();
	
	}

}
?>